<template>
	<view class="my-history-record">
		<view class="return-icon-title">
			<view class="top-left">
				<uni-icons type="left" size="28" @click="returnPreviewPage"></uni-icons>
			</view>

			<view class="top-center">
				<text class="scan-history-title" @click="openDropMenu">浏览记录</text>
				<image class="down-icon" src="@/static/icons/down.png" mode="aspectFill" v-show="!dropMenuShow"
					@click="openDropMenu"></image>
				<image class="up-icon" src="@/static/icons/up.png" mode="aspectFill" v-show="dropMenuShow"
					@click="openDropMenu"></image>
			</view>

			<view class="top-right">
				<text class="scan-history-manage" @click="changeManageState">{{manageOrCompleteText}}</text>
			</view>

		</view>
		<view class="history-content">
			<view class="segment">
				<uni-segmented-control :current="current" :values="items" @clickItem="onClickItem" styleType="text"
					activeColor="#66596b">
				</uni-segmented-control>
			</view>

			<view class="content">
				<view v-show="current === 0">
					<NoteShowList :notes="notes" :ManageMode="isManageMode" :selectAll="isAllChecked"
						@update-delete-note-aids="handleDeleteNoteAids" @update-all-select="updateAllSelectHandler" />
				</view>
				<view v-show="current === 1" class="goods">
					<view class="goods-item" v-for="(item,index) in goods" :key="index">
						<view class="image-goods" @click="toGoodsDetail(item)">
							<image :src="item.goodsImage" mode="aspectFill"></image>
						</view>
						<view class="goods-right-main">
							<view class="goods-right" @click="toGoodsDetail(item)">
								<view class="goods-name">
									{{item.goodsName}}
								</view>
								<view class="goods-scan-time">
									<uni-icons type="eye" color="#999999"></uni-icons>
									{{item.scanTime}}
								</view>
							</view>
							<view class="radio-view" v-show="isManageMode">
								<label class="radio">
									<radio :checked="item.isChecked" @click="clickRadioHandler(item)" color="#66596b" />
								</label>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>

		<view class="bottom-delete-bar" v-show="isManageMode">
			<view class="all-select">
				<label class="radio">
					<radio :checked="isAllChecked" @click="allSelect" color="#66596b" /><text>全选</text>
				</label>
			</view>
			<view class="delete-btn" @click="deleteHistory">
				<button type="default">删除</button>
			</view>
		</view>

		<!-- 选择一周之内，一月之内，三个月之内-->
		<uni-popup ref="popup" type="bottom" border-radius="10px 10px 0 0" @change="changeDropMenu">
			<view class="drop-down-menu-container">
				<view class="week-menu" @click="queryWeekHistory">
					一周之内
				</view>
				<view class="week-menu" @click="queryMonthHistory">
					一月之内
				</view>
				<view class="week-menu three-month" @click="queryThreeMonthHistory">
					三月之内
				</view>
			</view>
		</uni-popup>

	</view>
</template>

<script>
	import mitter from '@/common/mitt/eventBus.js';
	import NoteShowList from '@/components/common-components/NoteShowList.vue';
	export default {
		components: {
			NoteShowList,
		},
		data() {
			return {
				isManageMode: false, // 新增，用于标识是否处于管理模式（显示radio）
				dropMenuShow: false,
				manageOrCompleteText: '管理', // 管理或者完成
				items: ['笔记', '商品'],
				current: 0,
				notes: [{
						noteAid: 1,
						isVedio: false,
						url: 'https://img2.baidu.com/it/u=868566590,1876533926&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1139',
						title: '今天很忧郁',
						releaseTime: '2022年12月20日 23:23:04',
						isLike: false, // 是否点赞
						likeCount: 0,
						commentCount: 0,
					},
					{
						noteAid: 2,
						isVedio: true,
						url: 'https://fzg-jy.oss-cn-hangzhou.aliyuncs.com/jyapp/home/yufen/camping/video/%E5%85%85%E6%B0%94%E6%B2%99%E5%8F%915.mp4',
						videoCover: 'https://img0.baidu.com/it/u=195508433,693019668&fm=253&fmt=auto&app=120&f=JPEG?w=750&h=500',
						title: '家人们找到一个绝美的露营圣地',
						releaseTime: '2023年11月20日 23:08:04',
						isLike: false, // 是否点赞
						likeCount: 235,
						commentCount: 27,
					},
					{
						noteAid: 3,
						isVedio: true,
						url: 'https://fzg-jy.oss-cn-hangzhou.aliyuncs.com/jyapp/home/yufen/notes/范占国2024-12-07卷尺灯3.mp4',
						videoCover: 'http://t14.baidu.com/it/u=3871136798,171938325&fm=224&app=112&f=JPEG?w=500&h=500',
						title: '卷尺灯',
						releaseTime: '2024年8月20日 23:08:04',
						isLike: true, // 是否点赞
						likeCount: 345,
						commentCount: 90,
					},
					{
						noteAid: 4,
						isVedio: false,
						url: 'https://img0.baidu.com/it/u=3577356751,2936750237&fm=253&fmt=auto&app=120&f=JPEG?w=750&h=500',
						title: 'Sephoria 丝芙兰美妆节',
						releaseTime: '2024年10月20日 23:08:04',
						isLike: true, // 是否点赞
						likeCount: 56,
						commentCount: 0,
					},
					{
						noteAid: 5,
						isVedio: false,
						url: 'https://img0.baidu.com/it/u=364532874,2598986379&fm=253&fmt=auto&app=138&f=JPEG?w=607&h=405',
						title: '巷口的煎饼果子香',
						releaseTime: '2024年12月08日 08:56:00',
						isLike: true, // 是否点赞
						likeCount: 23,
						commentCount: 1,
					}
				],
				goods: [{
						goodsAid: 1,
						goodsName: '蛋黄酥',
						goodsImage: 'https://img1.baidu.com/it/u=3982039978,4115031592&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
						scanTime: '昨天 12:32',
						isChecked: false,
					},
					{
						goodsAid: 2,
						goodsName: '蛋黄酥',
						goodsImage: 'https://img1.baidu.com/it/u=3982039978,4115031592&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
						scanTime: '昨天 12:32',
						isChecked: false,
					},
					{
						goodsAid: 3,
						goodsName: '蛋黄酥',
						goodsImage: 'https://img1.baidu.com/it/u=3982039978,4115031592&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
						scanTime: '昨天 12:32',
						isChecked: false,
					},
					{
						goodsAid: 4,
						goodsName: '蛋黄酥',
						goodsImage: 'https://img1.baidu.com/it/u=3982039978,4115031592&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
						scanTime: '昨天 12:32',
						isChecked: false,
					},
					{
						goodsAid: 5,
						goodsName: '蛋黄酥',
						goodsImage: 'https://img1.baidu.com/it/u=3982039978,4115031592&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
						scanTime: '昨天 12:32',
						isChecked: false,
					},
				],
				isAllChecked: false, // 是否全选
				deleteNoteAids: [], // 用于接收子组件传递过来的要删除的笔记aid数组
				deleteGoodsAIids: [],
			};
		},
		
		
		methods: {
			handleDeleteNoteAids(data) {
				// 接收子组件传递过来的deleteNoteAids数据，并赋值给父组件中的deleteNoteAids属性
				this.deleteNoteAids = data;
			},
			returnPreviewPage() {
				uni.navigateBack()
			},
			changeManageState() {
				if (this.manageOrCompleteText === '管理') {
					this.manageOrCompleteText = '完成';
					this.isManageMode = true; // 设置为false，隐藏radio，退出管理模式
				} else {
					this.manageOrCompleteText = '管理';
					this.isManageMode = false; // 设置为true，显示radio，进入管理模式
				}
			},
			onClickItem(e) {
				if (this.current != e.currentIndex) {
					this.current = e.currentIndex;
				}
			},
			openDropMenu() {
				if (this.dropMenuShow) {
					this.$refs.popup.close()
				} else {
					this.$refs.popup.open()
				}
			},
			changeDropMenu(e) {
				this.dropMenuShow = e.show;
			},
			queryWeekHistory() {
				this.$refs.popup.close()
			},
			queryMonthHistory() {
				this.$refs.popup.close()
			},
			queryThreeMonthHistory() {
				this.$refs.popup.close()
			},
			toGoodsDetail(item) {
				uni.navigateTo({
					url: `/pages/tabBar/extUI/shopping_topBar/goodDetail?goodAid=${item.goodsAid}`
				})
			},
			allSelect() {
				this.deleteGoodsAIids = [];
				this.isAllChecked = !this.isAllChecked;
				this.notes.forEach(item => {
					item.isChecked = this.isAllChecked;
				});
				this.goods = this.goods.map(item => ({
					...item,
					isChecked: this.isAllChecked
				}));
				this.deleteGoodsAIids = this.isAllChecked ? this.goods.map(item => item.goodsAid) : [];
			},
			deleteHistory() {
				const that = this;
				if (that.current === 0) {
					// 当前删除的是笔记
					if (that.isAllChecked) {
						// 如果全选就直接删除
						uni.showModal({
							title: '提示',
							content: '确定删除吗？',
							cancelText: '再想想',
							confirmColor: '#66596b',
							success: function(res) {
								if (res.confirm) {
									that.notes = [];
								}
							}
						})
					} else {
						console.log("这里");
						console.log(this.deleteNoteAids);
						if (this.deleteNoteAids.length > 0) {
							uni.showLoading({
								title: '删除中...',
								success() {
									// 如果没有全选，就使用子组件传递过来的deleteNoteAids数组来进行删除操作
									that.notes = that.notes.filter(note => !that.deleteNoteAids.includes(note
										.noteAid));
									that.deleteNoteAids = [];
								}
							})
							setTimeout(function() {
								uni.hideLoading()
							}, 500);
						} else {
							uni.showToast({
								title: '请选择要删除的笔记',
								icon: 'none'
							})
						}
					}
				} else {
					if (that.isAllChecked) {
						uni.showModal({
							title: '提示',
							content: '确定删除吗？',
							cancelText: '再想想',
							confirmColor: '#66596b',
							success: function(res) {
								if (res.confirm) {
									that.goods = [];
								}
							}
						})
					} else {
						if (that.deleteGoodsAIids.length > 0) {
							uni.showLoading({
								title: '删除中...',
								success() {
									// 当前删除的是商品
									that.goods = that.goods.filter(good => !that.deleteGoodsAIids.includes(good
										.goodsAid));
									that.deleteGoodsAIids = []
								}
							})
							setTimeout(function() {
								uni.hideLoading()
							}, 500);
						}else {
							uni.showToast({
								title: '请选择要删除的商品',
								icon: 'none'
							})
						}
					}
				}
			},
			clickRadioHandler(item) {
				const aid = item.goodsAid;
				const index = this.deleteGoodsAIids.findIndex(existingAid => existingAid === aid);
				item.isChecked = !item.isChecked;
				index !== -1 ? this.deleteGoodsAIids.splice(index, 1) : this.deleteGoodsAIids.push(aid);
				this.isAllChecked = this.deleteGoodsAIids.length === this.goods.length;
			},
			updateAllSelectHandler(data) {
				this.isAllChecked = data
			},
		},
	}
</script>

<style lang="scss" scoped>
	.my-history-record {

		.return-icon-title {
			width: 100%;
			text-align: center;
			padding-top: 80rpx;
			position: fixed;
			top: 0;
			left: 0;
			z-index: 2;
			padding-bottom: 30rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			background-color: #f8f8f8;

			.top-left {
				margin-left: 20rpx;
			}

			.top-center {
				display: flex;
				align-items: center;

				.scan-history-title {
					font-size: 38rpx;
				}

				.down-icon {
					width: 50rpx;
					height: 50rpx;
				}

				.up-icon {
					width: 42rpx;
					height: 42rpx;
				}
			}

			.top-right {
				margin-right: 30rpx;

				.scan-history-manage {
					font-size: 32rpx;
					color: #66596b;
				}
			}
		}

		.history-content {
			width: 95%;
			margin-left: auto;
			margin-right: auto;
			margin-top: 300rpx;
			position: relative;

			.segment {
				width: 100vw;
				position: fixed;
				top: 160rpx;
				left: 0;
				background-color: #f8f8f8;
				z-index: 2;
				padding-bottom: 30rpx;
			}

			.content {
				margin-top: 40rpx;
				padding-bottom: 140rpx;

				.goods {
					width: 90%;
					margin-left: auto;
					margin-right: auto;

					.goods-item {
						display: flex;
						margin-bottom: 40rpx;
						background-color: #fff;
						padding: 20rpx;
						box-shadow: 0 0 10px 5px rgba(255, 255, 255, 0.6);

						.image-goods {
							image {
								width: 120rpx;
								height: 120rpx;
								border-radius: 10rpx;
							}
						}

						.goods-right-main {
							display: flex;
							justify-content: space-between;
							width: 85%;

							.goods-right {
								margin-left: 30rpx;

								.goods-name {
									letter-spacing: 2rpx;
								}

								.goods-scan-time {
									margin-top: 20rpx;
									font-size: 24rpx;
									color: #999999;
								}
							}
						}
					}
				}
			}
		}

		.bottom-delete-bar {
			width: 100%;
			height: 100rpx;
			position: fixed;
			left: 0;
			bottom: 0;
			background-color: #f8f8f8;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.all-select {
				margin-left: 30rpx;
			}

			.delete-btn {
				margin-right: 30rpx;

				button {
					width: 200rpx;
					background-color: #66596b;
					border-radius: 40rpx;
					color: #fff;
					letter-spacing: 2rpx;
					font-size: 28rpx;
				}
			}
		}


		.drop-down-menu-container {
			width: 100%;
			height: 25vh;
			background-color: #f8f8f8;
			border-radius: 10px 10px 0 0;
			display: flex;
			flex-direction: column;
			align-items: center;

			.week-menu {
				height: 120rpx;
				width: 100%;
				font-size: 32rpx;
				letter-spacing: 5rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				border-bottom: 1px solid #d9d9d9;
			}

			.three-month {
				border: none;
			}
		}
	}
</style>